package Q189;

import java.util.Arrays;

/**
 * 旋转数组
 * 给定一个数组，将数组中的元素向右移动 k 个位置，其中 k 是非负数。
 * @author 23737
 * @time 2021.11.9
 */
public class Test {
    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,4};
        int[] nums2 = new int[]{1,2,3,4};
//        new Solution().rotate(nums,2);
        new Solution().rotate2(nums2,2);
//        for (int num : nums) {
//            System.out.println(nums[num]);
//        }

        for (int num : nums2) {
            System.out.println(nums[num]);
        }
    }
}


class Solution {
    public void rotate(int[] nums, int k) {
        int len = nums.length;
        int[] ints = Arrays.copyOf(nums, len);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
        for (int i = 0; i < len; i++) {
            int pos = (i + k) % (len + 1);
            System.out.println("i-->" + i + " pos-->" + pos);
            nums[i] = ints[pos];
            System.out.println("nums[" + i + "]" + "--->" + nums[i]);
        }
    }

    public void rotate2(int[] nums, int k) {
        int n = nums.length;
        int[] newArr = new int[n];
        for (int i = 0; i < n; ++i) {
            newArr[(i + k) % n] = nums[i];
        }
        System.arraycopy(newArr, 0, nums, 0, n);
    }
}
